﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Upgrade
{
    class Program
    {
        private static readonly string APP_PATH = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
        private static readonly string APP_ROOT = System.IO.Path.GetDirectoryName(APP_PATH);
        private static readonly string DEST_PATH = APP_ROOT + "\\DownFile";
        private static readonly string AppName = "\\WoChu.RfClient.Entry.exe";

        static void Main(string[] args)
        {
              
            TraceEx.Write("开始执行");
            GC.Collect();
            //升级程序启动 --主程序退出--把Down目录下文档更新到目录中--关闭升级程序----主程序重新启动
            int processID = -1 ;
            try
            {
                TraceEx.Write("A");
                processID = Convert.ToInt32(args[0]);
                
            }
            catch
            {
                processID = -1 ;
            }

            if (processID > 0)
            {
                try
                {
                    TraceEx.Write("B");
                    System.Diagnostics.Process myProcess = System.Diagnostics.Process.GetProcessById(processID);
                    //myProcess.Kill();//强制关闭该程序 
                    if (myProcess != null)
                    {
                        TraceEx.Write("C");
                        myProcess.Kill();
                        //myProcess.Close();
                        //myProcess.Dispose();                        
                        //myProcess.WaitForExit();
                        //myProcess.Kill();

                        GC.Collect();
                        System.Threading.Thread.Sleep(2000);
                    }
                    TraceEx.Write("D");
                }
                catch (Exception ex)
                {
                    TraceEx.Write("EX");
                    TraceEx.Write(ex.Message);
                }
            }

            try
            {
                System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(DEST_PATH);
                if (folder.Exists)
                {
                    GC.Collect();
                    TraceEx.Write("开始拷贝覆盖文件！");
                    foreach (System.IO.FileInfo _file in folder.GetFiles("*.*"))
                    {
                        if (_file.Exists && !string.IsNullOrEmpty(_file.Extension))
                        {
                            string oldFile = APP_ROOT + @"\" + System.IO.Path.GetFileName(_file.FullName);
                            System.IO.File.Copy(_file.FullName, oldFile, true);
                        }
                         
                    }
                    TraceEx.Write("结束拷贝覆盖文件！");

                    // 修正版本
                    string version = AppSetting.NewInstance.GetString("VersionDesc");
                    AppSetting.NewInstance.SetString("Version", version);
                    AppSetting.NewInstance.Write();
                }

                System.Threading.Thread.Sleep(500);
            }
            catch (Exception ex)
            {
                TraceEx.Write("EX");
                TraceEx.Write(ex.Message);
            }
                

            //启动主重新
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(APP_ROOT + AppName, System.Diagnostics.Process.GetCurrentProcess().Id.ToString());
            if (startInfo != null && System.IO.File.Exists(startInfo.FileName))
            {
                System.Diagnostics.Process.Start(startInfo);
            }

            TraceEx.Write("更新程序结束！");
        }
        
    }
}
